﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pub.Common
{
    public static class ExtensionHelper
    {
        #region 时间扩展

        /// <summary>
        /// 时间格式获得中国星期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ChineseWeek(this DateTime date)
        {
            string enWeek = date.DayOfWeek.ToString();
            string cnWeek = string.Empty;
            switch (enWeek)
            {
                case "Monday":
                    cnWeek = "星期一";
                    break;
                case "Tuesday":
                    cnWeek = "星期二";
                    break;
                case "Wednesday":
                    cnWeek = "星期三";
                    break;
                case "Thursday":
                    cnWeek = "星期四";
                    break;
                case "Friday":
                    cnWeek = "星期五";
                    break;
                case "Saturday":
                    cnWeek = "星期六";
                    break;
                case "Sunday":
                    cnWeek = "星期日";
                    break;
            }
            return cnWeek;
        }

        /// <summary>
        /// Windows DateTime to Unix Timestamp
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static double ToTimestamp(this DateTime time)
        {
            DateTime? _time = time;
            return ToTimestamp(_time).Value;
        }
        /// <summary>
        /// Windows DateTime to Unix Timestamp
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static double? ToTimestamp(this DateTime? time)
        {
            if (time == null)
                return null;
            TimeSpan ts = (time.Value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
            return (int)ts.TotalSeconds;
        }
        /// <summary>
        ///  Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this long timestamp)
        {
            double? _timestamp = timestamp;
            return ToDateTime(_timestamp).Value;
        }
        /// <summary>
        ///  Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this long? timestamp)
        {
            double? _timestamp = timestamp;
            return ToDateTime(_timestamp);
        }
        /// <summary>
        ///  Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this int timestamp)
        {
            double? _timestamp = timestamp;
            return ToDateTime(_timestamp).Value;
        }
        /// <summary>
        ///  Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this int? timestamp)
        {
            double? _timestamp = timestamp;
            return ToDateTime(_timestamp);
        }
        /// <summary>
        ///  Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this double timestamp)
        {
            double? _timestamp = timestamp;
            return ToDateTime(_timestamp).Value;
        }
        /// <summary>
        /// Unix Timestamp to Windows DateTime
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this double? timestamp)
        {
            if (timestamp == null)
                return null;
            DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime reTime = startTime.AddSeconds(timestamp.Value);
            return reTime.ToLocalTime();
        }

        /// <summary>
        /// 时间格式化
        /// </summary>
        /// <param name="time"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string FormatDateTime(this DateTime time, string format = "yyyy-MM-dd")
        {
            DateTime? _time = time;
            return FormatDateTime(_time, format);
        }

        /// <summary>
        /// 时间格式化
        /// </summary>
        /// <param name="time"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string FormatDateTime(this DateTime? time, string format = "yyyy-MM-dd")
        {
            string timeStr = string.Empty;
            if (time != null)
            {
                timeStr = time.Value.ToString(format);
            }
            return timeStr;
        }

        /// <summary>
        /// 将8位，14位，17位纯数字字符串转换成日期时间格式
        /// 例如：20101010101010100 转换成：2010-10-10 10:10:10.100
        /// 熊学浩
        /// 2013-07-12
        /// </summary>
        /// <param name="time">例如：20101010101010100</param>
        /// <returns></returns>
        public static string FormatDateTime(this string time)
        {
            if (string.IsNullOrEmpty(time)) return string.Empty;
            StringBuilder _time = new StringBuilder();
            if (time.Length >= 8)
            {
                _time.Append(time.Substring(0, 4));
                _time.Append("-");
                _time.Append(time.Substring(4, 2));
                _time.Append("-");
                _time.Append(time.Substring(6, 2));
            }
            if (time.Length >= 14)
            {
                _time.Append(" ");
                _time.Append(time.Substring(8, 2));
                _time.Append(":");
                _time.Append(time.Substring(10, 2));
                _time.Append(":");
                _time.Append(time.Substring(12, 2));
            }
            if (time.Length >= 17)
            {
                _time.Append(".");
                _time.Append(time.Substring(14, 3));
            }
            return _time.ToString();
        }

        #endregion 时间扩展

        #region linq DistinctBy 扩展
        /// <summary>
        /// linq DistinctBy 扩展
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        /// 例：
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>
            (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element))) { yield return element; }
            }
        } 
        #endregion
    }

}
